<!DOCTYPE html>
<html>
<head>
    <title>ads form</title>
    <#include "/header.html">
    <style>
        .layui-upload-img {
            max-height: 100px;
        }
    </style>
</head>
<body class="bg-white">

<div id="appMain">
    <form class="layui-form layuimini-form" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label">头像</label>
            <div class="layui-input-block">
                <input type="hidden" name="avatar" value="https://mm-jianzhiba.oss-cn-shenzhen.aliyuncs.com/jianzhiba-upload/20210906/6b74353094854c31814abe8f84fa4c51.jpg">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="avatar" src="${request.contextPath}/statics/images/mengmao.jpg">
                        <p id="uploadText"></p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="输入用户名" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
      <div class="layui-form-item">
            <label class="layui-form-label">视频包目录</label>
            <div class="layui-input-block">
                <select id="videoPackageId" name="videoPackageId" lay-search>
      			</select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea name="content" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">评分</label>
            <div class="layui-input-block">
                <input type="text" name="score" lay-verify="required" lay-reqtext="评分不能为空" placeholder="输入1-5分" value="" class="layui-input" autocomplete="off">
            </div>
        </div>

        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal" id="commitBtn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    layui.use(['form', 'laytpl', 'upload'], function () {
        var form = layui.form,
            laytpl = layui.laytpl,
            upload = layui.upload,
            $ = layui.$;

        var _parent = parent;
        laytpl(_parent.layui.jquery("#positionListTpl").html()).render(_parent.positionList, function(html){
        	$("#positionListBox").html(html);
        	form.render("select");
            if(_parent.formDataId > 0) {
                $.ajax({
                    type: "POST",
                    url: serverUrl('video/comment/info/' + _parent.formDataId),
                    success: function (result) {
                        if (result.code === 0) {
                            form.val('dataForm', result.info);
                            $('#avatar').attr('src', result.info.avatar); //url
                        }
                    }
                });
            }
        });

        var submitForm = function () {
            var params = form.val('dataForm');
            params['id'] = _parent.formDataId || null;

            var method = _parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('app/video/comment/'+method, params);
        };

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#uploadBtn'
            ,url: serverUrl('oss/file/upload')
            ,auto: false
            ,choose: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#uploadPreview').attr('src', result); //base64
                });
            }
            ,done: function(res){
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                form.val('dataForm', {'image': res.url});
                submitForm();
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var params = data.field;
            params['id'] = _parent.formDataId || null;
            var method = _parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('video/comment/'+method, params, function (res) {
                
            });
            return false;
        });
      //获取视频包select
    	  $.ajax({
    			url:serverUrl('video/resources/packagetitle'),
    			type:"post",
    			dataType:"",
    			success:function(btn){
    		          var select = $("#videoPackageId");
    			      	for (var j = 0; j < btn.count; j++) {
    				      	  select.append("<option value='"+btn.data[j].id+"'>"+btn.data[j].title+"</option>");
    			  		}
    		          layui.form.render("select");
    			},
    			error:function(){
    				console.log("类型获取失败");
    			}
    		})
    });
</script>
</body>
</html>